<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseAdminController;
use Illuminate\Http\Request;
use App\Service\UserGroupService;
use App\Service\PostService;
class UserGroupController extends BaseAdminController{

    protected $userGroupService;
    protected $postService;

    public function _init(){
        $this->userGroupService = new UserGroupService();
        $this->postService = new PostService();
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request) {
        $params = $request->all();
        $data = $this->userGroupService->getUserGroupList($params);
        return view("admin.user_group.index", compact(['data']));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request) {
        if($request->isMethod("GET")){
            return view("admin.user_group.create");
        }else{
            $this->validateRequest($request,'create');
            $data = $request->except("_token");
            $result = $this->userGroupService->createData($data);
            return $this->success($result,"添加成功");
        }

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {

    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show() {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request) {
        if($request->isMethod("GET")){
            $id = $request->input('id');
            $data = $this->userGroupService->findData($id);
            return view("admin.user_group.update",compact(['data']));
        }else{
            $data = $request->except("_token");
            $this->validateData($data,'create');
            $result = $this->userGroupService->update($data['id'], $data);
            return $this->success($result, "编辑成功");
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */


    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        $list = $this->userGroupService->selectData(["group_id"=>$id]);
        if(count($list) > 0){
            return $this->layuiDataError(7000,"请先删除岗位分组中的内容");
        }
        $result = $this->userGroupService->delete($id);
        return $this->success($result, "删除成功");
    }
    public function userIndex(Request $request){
        $params = $request->all();
        $group_id = $request->input('group_id');
        $data = $this->userGroupService->getUserList($params);
        $list = $this->userGroupService->getUserData($group_id);
        return view("admin.user_group.user_index", compact(['data',"list"]));
    }

    public function userCreate(Request $request){
        $data = $request->except("_token");
        if(empty($data['value'])){
            $res['status'] = -1;
            $res['message'] = "请选择要添加的数据";
        }else{
            $res['status'] = 1;
            $res['message'] = "操作成功";
            $res['data'] = $this->userGroupService->createUser($data);
        }
        return $res;
    }
    public function userDestroy($id){
        $result = $this->userGroupService->deleteUser($id);
        return $this->success($result, "删除成功");
    }
}
